﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using LannyPractiseCode.Shu;

namespace LannyPractiseCode
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("开始。。。");
            TestHuffmanCode();
            //TestHuffmanTree();//测试哈夫曼树
            //List
            //MyHeapSort();
            //TestShu2();
            //TestShu();
            //TestHash();
            //TestFind()
            //TestSort();
            //TestMiGong();
            //TestQueen();
            //TestStack.TTTestStack();
            //TestStack();
            //TestYosefh();
            //TestDoubleLink();
            //TestSingleLink();
            //SortTesting();
            //BubbleSortTesting();
            //Fabonicc(4);
        }
        #region 树2 
        public static void TestHuffmanCode()
        {
            string content="i like like like java do you like a java";
            byte[] contentBytes=System.Text.Encoding.Default.GetBytes(content);
            var huffmanCodeNodes=HuffmanCode.GetNodes(contentBytes);
            /*foreach (var item in nodes)
            {
                System.Console.WriteLine(item);
            }*/

            //测试创建哈夫曼树
            HuffmanCodeNode node=HuffmanCode.CreateHuffmanTree(huffmanCodeNodes);
            //System.Console.Write(node+" ");
            //前序遍历输出
            HuffmanCode.PreOrder(node);

            //测试生成对应的哈夫曼编码
            //因为结点上面是空的，所以是  “”
            HuffmanCode.GetHuffmanCodes(node,"",HuffmanCode.stringBuilder);
            var aa=HuffmanCode.huffmanCodes;
            foreach (var item in HuffmanCode.huffmanCodes)
            {
                System.Console.WriteLine(item.Key+"="+item.Value);
            }

            //开始压缩。。
            byte[] HuffmanCodeBytes= HuffmanCode.Zip(contentBytes,HuffmanCode.huffmanCodes);
             foreach (var item in HuffmanCodeBytes)
             {
                 System.Console.Write(item+" ");
             }
        }

        //哈夫曼树
        public static void TestHuffmanTree()
        {
            int[] arr={13,7,8,3,29,6,1};
          HuffmanNode root=  HuffmanTree.CreateHuffumanTree(arr);
          HuffmanTree.PreOrder(root);
        }

        //HeapSort 堆排序
        public static void MyHeapSort()
        {
            System.Console.WriteLine("堆排序~~");
            int[] arr=new int[]{6,8,2,5,1,3};
            HeapSort.HeapSortHelper(arr);
        }

        static void TestShu2()
        {

            ChainTreeManager manager = new ChainTreeManager();

            ChainTree<string> tree = new ChainTree<string>() { data = "1" };

            ChainTree<string> node1 = new ChainTree<string>() { data = "2" };
            ChainTree<string> node2 = new ChainTree<string>() { data = "3" };
            ChainTree<string> node3 = new ChainTree<string>() { data = "5" };
            ChainTree<string> node4 = new ChainTree<string>() { data = "4" };

            manager.BinTreeAddNode(tree, node1, "1", Direction.Left);
            manager.BinTreeAddNode(tree, node2, "1", Direction.Right);
            manager.BinTreeAddNode(tree, node3, "3", Direction.Left);
            manager.BinTreeAddNode(tree, node4, "3", Direction.Right);

            manager.BinTree_MidOrder<string>(tree);



            /*
            ChainTreeManager manager = new ChainTreeManager();

            //插入节点操作
            ChainTree<string> tree = CreateRoot();
            //插入节点数据
            AddNode(tree);
            //先序遍历
            Console.WriteLine("\n先序结果为： \n");
            manager.BinTree_PreOrder(tree);

            //中序遍历
            Console.WriteLine("\n中序结果为： \n");
            manager.BinTree_MidOrder(tree);

            //后序遍历
            Console.WriteLine("\n后序结果为： \n");
            manager.BinTree_PostOrder(tree);

            //层次遍历
            Console.WriteLine("\n层次结果为： \n");
            //manager.Length = 100;
            //manager.BinTree_Level(tree);

            Console.WriteLine("\n树的深度为：" + manager.BinTreeLen(tree) + "\n");
            */

        }
        static ChainTree<string> CreateRoot()
        {
            ChainTree<string> tree = new ChainTree<string>();
            Console.WriteLine("请输入根节点，方便我们生成树\n");
            tree.data = Console.ReadLine();
            Console.WriteLine("根节点生成已经生成\n");
            return tree;
        }

        static ChainTree<string> AddNode(ChainTree<string> tree)
        {
            ChainTreeManager mananger = new ChainTreeManager();
            while (true)
            {
                ChainTree<string> node = new ChainTree<string>();
                Console.WriteLine("请输入要插入节点的数据：\n");
                node.data = Console.ReadLine();
                Console.WriteLine("请输入要查找的父节点数据：\n");
                var parentData = Console.ReadLine();
                if (tree == null)
                {
                    Console.WriteLine("未找到您输入的父节点，请重新输入。");
                    continue;
                }

                Console.WriteLine("请确定要插入到父节点的：1 左侧，2 右侧");
                Direction direction = (Direction)Enum.Parse(typeof(Direction), Console.ReadLine());
                tree = mananger.BinTreeAddNode(tree, node, parentData, direction);
                Console.WriteLine("插入成功，是否继续？  1 继续， 2 退出");

                if (int.Parse(Console.ReadLine()) == 1)
                    continue;
                else
                    break;
            }

            return tree;
        }
        #endregion
        #region 树
        public static void TestShu()
        {
            BinaryTree binaryTree = new BinaryTree();

            MyHeroNode root = new MyHeroNode(1, "Lanny1");
            MyHeroNode Hero2 = new MyHeroNode(2, "Lanny2");
            MyHeroNode hero3 = new MyHeroNode(3, "Lanny3");
            MyHeroNode hero4 = new MyHeroNode(4, "Lanny4");

            binaryTree.root = root;
            root.Left = Hero2;
            root.Right = hero3;
            hero3.Right = hero4;

            System.Console.WriteLine("删除测试");
            binaryTree.PreOrder();
            binaryTree.DeleNode(4);
            System.Console.WriteLine("删除后~~");
            binaryTree.PreOrder();
            /*
            System.Console.WriteLine("查找测试");
            binaryTree.Postrder();
            System.Console.WriteLine("******输出查找结果！~。。。");
            var node=binaryTree.PreOrderSearch(3);
            if(node!=null)
                System.Console.WriteLine(node.Id+",*****,name="+node.Name);
            else
                System.Console.WriteLine("没找到~~..");
            */
        }


        #endregion
        #region 哈希
        public static void TestHash()
        {
            MyHashTable hashTable = new MyHashTable(10);
            Employee emp1 = new Employee(1, "Lanny1");
            Employee emp2 = new Employee(2, "Lanny2");
            Employee emp3 = new Employee(11, "Lanny11");
            Employee emp33 = new Employee(33, "Lanny33");
            hashTable.Add(emp1);
            hashTable.Add(emp2);
            //hashTable.Display();
            hashTable.Add(emp3);
            hashTable.Add(emp33);
            hashTable.Display();

            hashTable.FindEmplyeeByID(33);



        }
        #endregion
        #region 查找
        public static void TestFind()
        {
            List<int> arr = new List<int>();
            for (int iii = 0; iii < 100; iii++)
            {
                arr.Add(iii + 1);
            }

            //插值查找
            int i = SearchHelper.InsertSearch(arr, 0, arr.Count - 1, 99);
            System.Console.WriteLine("所在位置=" + i);
        }
        #endregion
        #region 排序
        public static void TestSort()
        {
            int max = 8;
            List<int> arrList = new List<int>(max);
            for (int i = 0; i < max; i++)
                arrList.Add(new Random().Next(0, max));
            //System.Console.WriteLine(string.Join(",",arrList));
            Stopwatch watch1 = new Stopwatch();
            watch1.Start();
            SortHelper.MergeSort(arrList.ToArray(), 0, arrList.Count - 1);
            watch1.Stop();
            TimeSpan ts2 = watch1.Elapsed;
            Console.WriteLine("总共花费{0}ms.", ts2.TotalMilliseconds);


            System.Console.WriteLine("排序结果：");
            //Console.WriteLine(string.Join(",", arrList));
        }
        #endregion
        #region 递归
        public static void TestQueen()
        {
            Queen8 queen = new Queen8();
            queen.BeginFind(0);
        }

        public static void TestMiGong()
        {
            MiGong queen = new MiGong();
            queen.RunMiGong();
        }


        #endregion
        #region Stack And Queue
        public static void TestStack11()
        {
            ArrayStack stack = new ArrayStack(4);
            stack.Push(10);
            stack.Push(3);
            stack.Push(31);
            stack.DisplayStack();
            stack.Pop();
            stack.DisplayStack();
        }
        #endregion
        #region 约瑟夫链表
        public static void TestYosefh()
        {
            System.Console.WriteLine("约瑟夫链表");
            CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
            circleSingleLinkedList.Add(5);
            circleSingleLinkedList.ShowBoy();

            circleSingleLinkedList.AcountBoy(1, 2, 5);
        }
        #endregion
        #region 双向链表测试
        public static void TestDoubleLink()
        {
            DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
            HeroNode2 node1 = new HeroNode2(1, "Lanny1", "Lannyyyy1");
            HeroNode2 node2 = new HeroNode2(2, "Lanny2", "Lannyyyy2");
            HeroNode2 node3 = new HeroNode2(3, "Lanny3", "Lannyyyy3");
            HeroNode2 node4 = new HeroNode2(4, "Lanny3", "Lannyyyy3");

            doubleLinkedList.Add(node1);
            doubleLinkedList.Add(node2);
            doubleLinkedList.Add(node3);
            doubleLinkedList.Add(node4);

            doubleLinkedList.Display();

            System.Console.WriteLine("修改节点");
            HeroNode2 updateNode = new HeroNode2(3, "Lanny33333", "Lannyyyy33333");
            doubleLinkedList.UpdateNode(updateNode);
            doubleLinkedList.Display();

            System.Console.WriteLine("删除节点");
            doubleLinkedList.DeleteNode(2);
            doubleLinkedList.Display();

        }
        #endregion
        #region 单链表
        public static void TestSingleLink()
        {
            SingleLinkedList singleLinkedList = new SingleLinkedList();
            HeroNode node1 = new HeroNode(1, "Lanny1", "Lannyyyy1");
            HeroNode node2 = new HeroNode(2, "Lanny2", "Lannyyyy2");
            HeroNode node3 = new HeroNode(3, "Lanny3", "Lannyyyy3");

            /*
            singleLinkedList.Add(node1);
            singleLinkedList.Add(node3);
            singleLinkedList.Add(node2);*/

            singleLinkedList.AddByOrder(node2);
            singleLinkedList.AddByOrder(node1);
            singleLinkedList.AddByOrder(node3);

            //修改
            //HeroNode updateNode=new HeroNode(3,"Lanny3","LannAAA3");
            //singleLinkedList.UpdateNode(updateNode);

            singleLinkedList.Display();

            //singleLinkedList.DeleteNode(2);

            //var iLength=singleLinkedList.GetNodesLength(singleLinkedList.GetHeadNode());
            //System.Console.WriteLine("个数："+iLength.ToString());
            //HeroNode heroNode=singleLinkedList.GetLastIndexNode(singleLinkedList.GetHeadNode(),2);
            //System.Console.WriteLine(heroNode.Name);

            //System.Console.WriteLine("打印反向打印。。");
            //SingleLinkedListTest.ReversePrint(singleLinkedList.GetHeadNode());

            System.Console.WriteLine("反转");
            SingleLinkedListTest.ReverseList(singleLinkedList.GetHeadNode());
            singleLinkedList.Display();

        }
        #endregion
        #region 排序算法测试
        public static void SortTesting()
        {
            List<int> arrList = new List<int>() { 9, 4, 2, 1, 7, 5 };
            System.Console.WriteLine("****排序前结果显示****");
            System.Console.WriteLine(string.Join(',', arrList));
            //SortHelper.MergeSort(arrList.ToArray(),0,arrList.Count-1);
            SortHelper.InsertSort(arrList);
            System.Console.WriteLine("****排序后结果显示****");
            System.Console.WriteLine(string.Join(',', arrList));
        }
        #endregion
    }
}
